package  UI 
{  
	
	import flash.display.Sprite;  
	import flash.events.Event; 
	import flash.events.MouseEvent;  
	import flash.geom.Rectangle;  
	import flash.text.TextField;  
	import flash.text.TextFormat;  
	import flash.filters.GlowFilter;	
	
	
	public class UITextArea extends Sprite   
		
	{  		
		private var tf:TextField;  		
		private var maxline:int = 0;  	
		private var slide:Sprite;  //滑块  
			
		public function UITextArea(width:int=300,height:int=80)   		
		{  			
			super();  			
			tf = new TextField();  			
			tf.width = width - 15;   //15px是滚动条  			
			tf.height = height;  			
			tf.border = false;  			
			tf.borderColor = 0x000000;  			
			tf.background = false;  			
			tf.textColor = 0xff0000;  			
			tf.defaultTextFormat = new TextFormat("宋体", 13, 0xFF0000);  			
			tf.selectable = false;  			
			tf.multiline = true;  			
			tf.wordWrap = true;  
			tf.filters = [new GlowFilter(0x000000,1,1.5,1.5,100)];
						
			this.graphics.beginFill(0x111111, 0.2);  		
			this.graphics.lineStyle(1, 0x000000);  		
			this.graphics.moveTo(width - 16, 0);  		
			this.graphics.lineTo(width - 1, 0);  		
			this.graphics.lineTo(width - 1, height);  		
			this.graphics.lineTo(width - 16, height);  		
			this.graphics.endFill();  			
			
			slide = new Sprite();  		
			PaintSlide(0x12aad6);  			
			slide.x = width - 14;  
			slide.y = 1;  		
			slide.visible = false;  		
			
			addChild(tf);  		
			addChild(slide);  			
			tf.addEventListener(Event.SCROLL, onWheel);  			
			slide.addEventListener(MouseEvent.MOUSE_DOWN, onDown);  
			
		}  
		
		//设置做多显示行  
		
		public function set maxLine(mx:int):void 			
		{  			
			maxline = mx;  			
		}  
		
		public function set backColor(col:uint):void 		
		{  		
			tf.backgroundColor = col;  		
		}  
				
		public function addLine(src:String):void 		
		{  			
			tf.appendText(src+ "\n");  			
			if (maxline > 0 && tf.numLines > maxline+1)  { //1行对应numlines为2  			
				tf.replaceText(0, tf.getLineOffset(1), "");   //删去第一行  			
			}  
			
			tf.scrollV = tf.maxScrollV;   //滚动到末行  		
			if (tf.numLines > tf.bottomScrollV - tf.scrollV + 2)  //动态显隐滚动条  				
			{
				slide.visible = true;  
				
			}else 
			{  
				slide.visible = false;  		
			}  		
		}  
		
		public function Clear():void 		
		{  		
			tf.text = "";  		
		}  
		
		//根据颜色填充滑块  	
		private function PaintSlide(col:uint):void			
		{  		
			slide.graphics.beginFill(col);  		
			if (this.height < 60)   //高度调节  
			{	
				slide.graphics.drawRoundRect(0, 0, 13, 10, 6, 6);  
				
			}
			else  
			{ 	
				slide.graphics.drawRoundRect(0, 0, 13, 20, 6, 6);  
				
			}  
			
			slide.graphics.endFill();  
			
		}  
		
		//tf自动滚动，此处更新滚动条位置  	
		private function onWheel(e:Event):void 	
		{  		
			slide.y = (this.height - slide.height - 2) * (tf.scrollV-1) / (tf.maxScrollV-1)+1;  		
		}  
		
		private function onDown(e:MouseEvent):void 	
		{  	
			slide.startDrag(false, new Rectangle(width - 13, 1,0, this.height - slide.height-2));  	
			PaintSlide(0x0f8cb0);  
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);  		
			slide.addEventListener(MouseEvent.MOUSE_OUT, onUp);   //out事件限定在滑块中触发有效  
			
			this.addEventListener(MouseEvent.MOUSE_UP, onUp);  
			
		}  
		
		//进此函数时，滑块一定在拖动过程中，无需判断  
		
		private function onMove(e:MouseEvent):void 		
		{  		
			tf.scrollV = Math.ceil(tf.maxScrollV * slide.y/(this.height-slide.height-2)); 			
		}  
		
		
		
		private function onUp(e:MouseEvent):void 		
		{  		
			slide.stopDrag();  			
			PaintSlide(0x12aad6);  			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);  		
			this.removeEventListener(MouseEvent.MOUSE_UP, onUp);  		
			slide.removeEventListener(MouseEvent.MOUSE_OUT, onUp);  			
		}  
	}  
} 